package com.xml.encrypt.util;

/**
 * Created by lelexiao on 2018/4/21.
 */


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.charset.Charset;


public class DesUtil {


    /**
     * 解密
     *
     * @param hexStr
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String hexStr, Charset charset, String sKey) throws Exception {
        byte[] src = Des.parseHexStr2Byte(hexStr);
        byte[] buf = Des.decrypt(src, sKey);
        return new String(buf, charset);
    }

    public  static  void strToFile(String src,String dest ) throws Exception {
        FileOutputStream out = new FileOutputStream(dest);
        out.write(src.getBytes());
        out.flush();
        out.close();
    }



    private static final Charset CHARSET = Charset.forName("UTF-8");

    public  static  void saveFileNew (File mCascadeFile , InputStream inputStream,String kk )   {
        try {

            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            String str  =result.toString("UTF-8");

            String   decryResult = DesUtil.decrypt(str , CHARSET, kk);
            DesUtil.strToFile(decryResult ,mCascadeFile.getAbsolutePath() );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
